package com.service.impl;

import com.service.SHA256Service;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName SHA256Service
 * @Description TODO
 * @Author milkywayeye
 * @Date 2023/11/1 17:49
 * @Version V1.0
 **/
@Service
public class SHA256ServiceImpl implements SHA256Service {
//    靠谱
    private String serviceKey = "662e5e8ab3bf402b9371257db7253730";

//    private String serviceKey = "9df75b2c58a4457b91857371a978e916";

    public String encryption(String key) {
        key = serviceKey + key;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(key.getBytes());

            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}
